/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import vanScheduling.Vehicle;
import vanScheduling.VehicleType;

/**
 *
 * @author user
 */
public class VehicleTypeDAO extends DAOTest {

    public synchronized static boolean addVehicleType(VehicleType vt) {
        Connection con = getConnection();
        boolean result = false;

        try {
            CallableStatement cs = con.prepareCall("{call dbo.stp_insVehicleType(?, ?)}");
            cs.setString(1, vt.getName());
            cs.setInt(2, vt.getNumOfSeats());
            int added = cs.executeUpdate();

            result = added > 0;

            cs.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static List<VehicleType> getVehicleTypes() {
        Connection con = getConnection();
        ArrayList<VehicleType> vehicleTypes = new ArrayList<VehicleType>();
        try {
            CallableStatement cs = con.prepareCall("{call stp_selAllVehicleTypes}");
            ResultSet rs = cs.executeQuery();

            while (rs.next()) {
                int id = rs.getInt("ID");
                String name = rs.getString("Name");
                int numOfSeats = rs.getInt("NumOfSeats");
                vehicleTypes.add(new VehicleType(id, name, numOfSeats));
            }
            cs.close();
            con.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return vehicleTypes;
    }

    public synchronized static boolean deleteVehicleType(VehicleType vehicleType)
    {
        Connection con = getConnection();
        boolean result = false;
        try
        {
            CallableStatement cs = con.prepareCall("{call stp_delVehicleType(?)}");
            cs.setInt(1, vehicleType.getId());
            int delete = cs.executeUpdate();

            result = delete > 0;
        }
        catch (SQLException e) {System.err.println(e.getMessage());}
        return result;
    }
}
